<!DOCTYPE>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" id="viewport" content="user-scalable=no,width=device-width" />
<link rel="stylesheet" href="css/project/style.css">
<link rel="stylesheet" href="css/zv_common.css">
<link rel="stylesheet" href="css/airport.css">
<script type="text/javascript" src="js/zy_control.js"></script>
<script type="text/javascript" src="js/zy_click.js"></script>
<script type="text/javascript" src="js/main.js"></script>
<script type="text/javascript" src="js/JsonSQL/jsonsql-0.1.js"></script>
<script src="js/talkingdata.js"></script>
<script>
		function search(val)
		{
			list(val);
		}
</script>

</head>
<body>
<div id="Index" style="position:fixed;color:#003079;dispaly:none;font-size:5em; z-index:999999; text-align:center; top:42%; width:100%"></div>
<textarea id="data" style="display:none;"></textarea>
<input id="lengthStr" type="hidden">
<div id="content">
	<div id="page"></div>
</div>
<div id="indexbar"></div>
<script src="js/airportJS/app.js"></script>
<script>
var titHeight = parseInt(localStorage.getItem("titHeight"));
var nStartY;
var nStartX;
var obj  = document.getElementById("indexbar");
 obj.addEventListener("touchstart", function (e) {
 		$("#indexbar").addClass("indexbaract");
		nStartY = e.targetTouches[0].pageY;
    	nStartX = e.targetTouches[0].pageX;
		e.preventDefault();
}, false);
 obj.addEventListener("touchend", function (e) {
 	setTimeout(function(){document.getElementById("Index").style.display="none"},3000);
 		$("#indexbar").removeClass("indexbaract");
}, false);

 document.addEventListener("touchend", function (e) {
 	setTimeout(function(){document.getElementById("Index").style.display="none"},3000);
}, false);

window.onload=function(){
	IC.talkingDataCount("机场三码/四码/名称/拼音查询","0014");
	Loading(0);
	$.getJSON("data/airport.json", function(jjson){
		document.getElementById("data").value = JSON.stringify(jjson);
		arrIndex=new Array();
		list("");
	});
}
//Q.ready(function(){
//list("");
//});
function setAirPorts(name,code)
{
	IC.talkingDataCount("机场三码/四码/名称/拼音查询","0014");
	uescript("airport","setAirPort('"+name+"','"+code+"')");
}

function list(key)
{

	var jsonstr = document.getElementById("data").value;
	var jjson = JSON.parse(jsonstr);
	arr=new Array();
	str="";
	istr="";
	var k=0;
	for(var i=64;i<=90;i++){
		if (String.fromCharCode(i) == "@") {
			var res = jsonsql.query("select * from json.data.items where (Tag=='1')", jjson);
		}
		else
		{
			var res = jsonsql.query("select * from json.data.items where (FirstLetter=='" + String.fromCharCode(i) + "')", jjson);
		}
		
		if(res.length>0)
		{
			k++;
			arrIndex[k-1]=String.fromCharCode(i).replace("@","<span style=color:red>★</span>");
			str+="<dl><dt>"+(String.fromCharCode(i).replace("@","重要航班"))+"</dt>";
		}
		for(var j=0;j<res.length;j++)
		{
			if(key.length>0)
			{
				if(res[j].FullName.indexOf(key)!=-1)
				{
					str += "<dd ontouchstart=\"zy_touch('zvbtn-act')\" onclick=\"setAirPorts('"+res[j].FullName+"','"+res[j].IATACode+"')\">" + res[j].FullName + "(" + res[j].IATACode + ")</dd>";
				}
			}
			else
			{
				str += "<dd ontouchstart=\"zy_touch('zvbtn-act')\" onclick=\"setAirPorts('"+res[j].FullName+"','"+res[j].IATACode+"')\">" + res[j].FullName + "(" + res[j].IATACode + ")</dd>";
			}
		}
		str+="</dl>";
		
		if(res.length>0)
		{
				istr+="<li>"+String.fromCharCode(i).replace("@","<span style=color:red>★</span>")+"</li>";
		}
	}
	$("#lengthStr").val(k);
	$("#page").html(str);
	Loading(-1);
	$("#indexbar").html("<ul>"+istr+"</ul>");
	lengthStr = parseInt($("#lengthStr").val());
	titHeight = localStorage.getItem("titHeight");
	height=parseInt((parseInt(document.body.offsetHeight)-titHeight)/lengthStr);
	$("#indexbar li").css({
		"height": height + "px",
		"line-height": height + "px"
	});
	 obj.addEventListener("touchmove", function (e) {
		var touch = e.targetTouches[0]; 
		var yyy = touch.screenY;
		var clientY = parseInt(Math.round(touch.screenY));
		
		var y = Math.round((clientY-titHeight/lengthStr)/lengthStr);
		
		if(y>=0&&y<lengthStr)
		{
			document.getElementById("Index").style.display="block";
			document.getElementById("Index").innerHTML=arrIndex[y];
			$("body").scrollTop($("#indexbar li").eq(parseInt(y)).attr("rel"));
			dostyle(parseInt(y));
		}
		e.preventDefault();
	 }, false);
	$("#page dt").each(function(data){
		itop=$(this).offset().top*Q.getZoom();
		arr[data]=itop;
		$("#indexbar li").eq(data).attr("rel",itop).bind("touchstart click",function(){
			document.getElementById("Index").innerHTML=arrIndex[data];
			$("body").scrollTop($("#indexbar li").eq(data).attr("rel"));			
			dostyle(data);
		});
	});
	window.onscroll=function(){
		bodytop=$("body").scrollTop();
		for(var i=0;i<=arr.length;i++){
			if(bodytop<arr[i]){
				dostyle(i-1);
				exit;
			}
		}
	};
}

function dostyle(id){
	id=id<0?0:id;
	//$("#indexbar li").removeClass("on").eq(id).addClass("on");
	document.getElementById("Index").innerHTML=arrIndex[id];
	document.getElementById("Index").style.display="block";
}
</script>
</body>
</html>